using System;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using SolutionFactory.Services;

namespace SolutionFactory.Exporter
{
    public class Logger : ILogger
    {
        private readonly IVsOutputWindow _outputWindow;
        private IVsOutputWindowPane _pane;

        public Logger(IVsOutputWindow outputWindow)
        {
            _outputWindow = outputWindow;
            
            if(GetPane() == null)
            {
                CreatePane();
            }

            Log("Logger initialized");
        }

        private void CreatePane()
        {
            Guid paneGuid = VSConstants.GUID_OutWindowGeneralPane;
            _outputWindow.CreatePane(ref paneGuid, "General", 1, 1);
        }

        public void Log(string Message)
        {
            if (_pane == null)
                _pane = GetPane();

            if (_pane != null)
            {
                _pane.OutputStringThreadSafe(Message + "\r\n");
            }
        }

        private IVsOutputWindowPane GetPane()
        {
            Guid generalPaneGuid = VSConstants.GUID_OutWindowGeneralPane;

            IVsOutputWindowPane pane;
            _outputWindow.GetPane(ref generalPaneGuid, out pane);
            return pane;
        }
    }
}